package company.beike;

import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class BeiKe3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int city = sc.nextInt();
        int n = sc.nextInt();
        sc.nextLine();
        Map<Long, String> dict = new TreeMap<>();
        int[][] test = new int[n][4];
        for (int i = 0; i < n; i++) {
            String[] s1 = sc.nextLine().split(" ");
            dict.put(getCount(Integer.parseInt(s1[1]), Integer.parseInt(s1[2])), s1[0] + "_" + s1[1]);
        }
        int count = 0;
        int[] citys = new int[n + 1];
        long result = 0;
        while (count != city - 1) {
            for (Map.Entry<Long, String> e : dict.entrySet()) {
                String[] s = e.getValue().split("_");
                int start = Integer.parseInt(s[0]);
                int end = Integer.parseInt(s[1]);
                if (citys[start] == 0 || citys[end] == 0) {
                    citys[start] = 1;
                    citys[end] = 1;
                    count++;
                    result += e.getKey();
                }
            }
        }
        System.out.println(result % (Math.pow(10, 9) + 7));
    }

    private static long getCount(int a, int b) {
        long result = 0;
        int i = a;
        while (i > b) {
            result *= i;
            i--;
        }
        return result;
    }
}
